package com.dzl.card;

import com.dzl.base.controller.BaseController;
import com.dzl.card.pojo.dto.WelfareCardTypeDto;
import com.dzl.card.pojo.query.WelfareCardTypeQuery;
import com.dzl.card.pojo.vo.WelfareCardTypeVo;
import com.dzl.card.service.WelfareCardTypeService;
import com.dzl.util.EmptyUtils;
import com.dzl.util.JsonResult;
import com.dzl.util.PageUtils;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * 卡类型表(WelfareCardType)表控制层
 * @author caobucao
 * @since 2021-05-18 18:45:01
 */
@Api(value = "企业端-卡类型管理", tags = "企业端-卡类型管理")
@RestController
@RequestMapping("welfareCardType")
public class WelfareCardTypeController extends BaseController {

    @Autowired
    private WelfareCardTypeService service;

    /**
     * 分页查询卡类别列表
     * @date 2021/5/18 19:00
     * @author caobucao
     * @param query 查询条件
     * @return com.dzl.util.PageUtils<com.dzl.cardType.entity.vo.WelfareCardTypeVo>
     */
    @ApiOperation(value = "企业端-分页查询卡类别列表", notes = "企业端-分页查询卡类别列表")
    @GetMapping("queryWelfareCardTypeListPage")
    public PageUtils<WelfareCardTypeVo> queryWelfareCardTypeListPage(WelfareCardTypeQuery query) {
        query.setEnterpriseId(getAdminInfo().getEnterpriseId());
        PageInfo<WelfareCardTypeVo> pageInfo = service.selectWelfareCardTypeListPage(query);
        return new PageUtils<>(pageInfo);
    }

    /**
     * 新增/编辑卡类型
     * @date 2021/5/18 19:14
     * @author caobucao
     * @param welfareCardTypeDto 卡类型传输对象
     * @return com.dzl.util.JsonResult<java.lang.Integer>
     */
    @ApiOperation(value = "企业端-新增/编辑卡类型", notes = "企业端-新增/编辑卡类型")
    @PostMapping("saveWelfareCardType")
    public JsonResult<Integer> saveWelfareCardType(@RequestBody WelfareCardTypeDto welfareCardTypeDto) {
        if(EmptyUtils.isEmpty(welfareCardTypeDto)) {
            return JsonResult.paramErrWithMsg("请填写内容");
        }
        if(EmptyUtils.isEmpty(welfareCardTypeDto.getName())) {
            return JsonResult.paramErrWithMsg("请填写卡类型名称");
        }
        if(EmptyUtils.isEmpty(welfareCardTypeDto.getUseDescription())) {
            return JsonResult.paramErrWithMsg("请填写卡类型说明");
        }
        if(EmptyUtils.isEmpty(welfareCardTypeDto.getId())) {
            welfareCardTypeDto.setUpdater(getAdminInfo().getId());
            welfareCardTypeDto.setUpdaterName(getAdminInfo().getUserName());
            welfareCardTypeDto.setCreater(getAdminInfo().getId());
            welfareCardTypeDto.setCreaterName(getAdminInfo().getUserName());
            if(welfareCardTypeDto.getShowClassify()==null) {
                return JsonResult.paramErrWithMsg("请选择分类入口是否展示");
            }
            welfareCardTypeDto.setEnterpriseId(getAdminInfo().getEnterpriseId());
        } else {
            welfareCardTypeDto.setUpdater(getAdminInfo().getId());
            welfareCardTypeDto.setUpdaterName(getAdminInfo().getUserName());
            welfareCardTypeDto.setUpdateTime(new Date());
            welfareCardTypeDto.setShowClassify(null);
        }
        return service.saveWelfareCardType(welfareCardTypeDto) <= 0 ? JsonResult.paramErrWithMsg("添加失败！") : JsonResult.success();
    }

    /**
     * 通过卡类型id删除卡类型
     * @date 2021/7/30 10:08
     * @author caobucao
     * @param id 卡类型id
     * @return com.dzl.util.JsonResult<java.lang.Integer>
     */
    @ApiOperation(value = "企业端-通过卡类型id删除卡类型", notes = "企业端-通过卡类型id删除卡类型")
    @DeleteMapping("deleteCardType")
    public JsonResult<Integer> deleteCardType(Integer id) {
        return JsonResult.success(service.deleteCardType(id));
    }

}
